"""
工具库
"""

import json
import zipfile
import os


def dfs_match(src, dist):
    # print(list(src.keys()), list(dist.keys()))
    for key in src:
        print("comparing:", key)
        if key not in dist:
            return False
        if type(dist[key]) is dict and type(src[key]) is dict:
            return dfs_match(src[key], dist[key])
    return True


def make_results(code: int, message: str, data=None):
    result = {
        'code': code,
        'message': message
    }
    if data is not None:
        result['data'] = data
    return result


def make_errors(error, data=None):
    return make_results(error['code'], error['message'], data)


def app_info_check(data):
    with open('require-data.json', 'r') as f:
        least_data = json.load(f)
        return dfs_match(least_data, data)


def un_zip(zip_file, target):
    """unzip zip file"""
    # zip_file = zipfile.ZipFile(file_name)
    if os.path.isdir(target):
        pass
    else:
        os.mkdir(target)
    for names in zip_file.namelist():
        zip_file.extract(names, target)
    zip_file.close()


if __name__ == '__main__':
    _dist = {
  "info": {
    "name": "simple",
    "display-name": "简单示例应用",
    "command": "python simple",
    "run-mode": "once-start",
    "version-info": {
      "owner": "Chiro",
      "version": "0.01",
      "requirements": [
        "flask"
      ],
      "comments": "只是一个简单的web应用示例，包含一个主页。"
    }
  }
}
    _src = {
  "info": {
    "name": "simple",
    "command": "python simple",
    "run-mode": "once-start",
    "version-info": {
      "owner": "Chiro",
      "version": "0.01",
      "requirements": [
        "flask"
      ]
    }
  }
}
    print(dfs_match(_src, _dist))